package cn.stylefeng.guns.modular.fileAssociation.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.fileAssociation.entity.FileAssociation;
import cn.stylefeng.guns.modular.fileAssociation.mapper.FileAssociationMapper;
import cn.stylefeng.guns.modular.fileAssociation.model.params.FileAssociationParam;
import cn.stylefeng.guns.modular.fileAssociation.model.result.FileAssociationResult;
import cn.stylefeng.guns.modular.fileAssociation.service.FileAssociationService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件关联 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-21
 */
@Service
public class FileAssociationServiceImpl extends ServiceImpl<FileAssociationMapper, FileAssociation> implements FileAssociationService {

    @Override
    public void add(FileAssociationParam param) {
        FileAssociation entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FileAssociationParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(FileAssociationParam param) {
        FileAssociation oldEntity = getOldEntity(param);
        FileAssociation newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FileAssociationResult findBySpec(FileAssociationParam param) {
        return null;
    }

    @Override
    public List<FileAssociationResult> findListBySpec(FileAssociationParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FileAssociationParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void addFileAssociation(Long fileId, Integer type, Long sourceId) {
        FileAssociation fileAssociation = new FileAssociation();
        fileAssociation.setFileId(fileId);
        fileAssociation.setType(type);
        fileAssociation.setSourceId(sourceId);
        save(fileAssociation);
    }

    @Override
    public List<File> listFile(Integer type, Long sourceId) {
        return this.baseMapper.listFile(type, sourceId);
    }

    private Serializable getKey(FileAssociationParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FileAssociation getOldEntity(FileAssociationParam param) {
        return this.getById(getKey(param));
    }

    private FileAssociation getEntity(FileAssociationParam param) {
        FileAssociation entity = new FileAssociation();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
